{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Maximize the Topmost Element After K Moves"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maximumTop"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #K 次操作后最大化顶端元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;，它表示一个 <strong>栈</strong> ，其中 <code>nums[0]</code>&nbsp;是栈顶的元素。</p>\n",
    "\n",
    "<p>每一次操作中，你可以执行以下操作 <strong>之一</strong>&nbsp;：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果栈非空，那么 <strong>删除</strong>&nbsp;栈顶端的元素。</li>\n",
    "\t<li>如果存在 1 个或者多个被删除的元素，你可以从它们中选择任何一个，<b>添加</b>&nbsp;回栈顶，这个元素成为新的栈顶元素。</li>\n",
    "</ul>\n",
    "\n",
    "<p>同时给你一个整数&nbsp;<code>k</code>&nbsp;，它表示你总共需要执行操作的次数。</p>\n",
    "\n",
    "<p>请你返回 <strong>恰好</strong>&nbsp;执行 <code>k</code>&nbsp;次操作以后，栈顶元素的 <strong>最大值</strong>&nbsp;。如果执行完 <code>k</code>&nbsp;次操作以后，栈一定为空，请你返回 <code>-1</code>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>nums = [5,2,2,4,0,6], k = 4\n",
    "<b>输出：</b>5\n",
    "<strong>解释：</strong>\n",
    "4 次操作后，栈顶元素为 5 的方法之一为：\n",
    "- 第 1 次操作：删除栈顶元素 5 ，栈变为 [2,2,4,0,6] 。\n",
    "- 第 2 次操作：删除栈顶元素 2 ，栈变为 [2,4,0,6] 。\n",
    "- 第 3 次操作：删除栈顶元素 2 ，栈变为 [4,0,6] 。\n",
    "- 第 4 次操作：将 5 添加回栈顶，栈变为 [5,4,0,6] 。\n",
    "注意，这不是最后栈顶元素为 5 的唯一方式。但可以证明，4 次操作以后 5 是能得到的最大栈顶元素。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>nums = [2], k = 1\n",
    "<b>输出：</b>-1\n",
    "<b>解释：</b>\n",
    "第 1 次操作中，我们唯一的选择是将栈顶元素弹出栈。\n",
    "由于 1 次操作后无法得到一个非空的栈，所以我们返回 -1 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>0 &lt;= nums[i], k &lt;= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [maximize-the-topmost-element-after-k-moves](https://leetcode.cn/problems/maximize-the-topmost-element-after-k-moves/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [maximize-the-topmost-element-after-k-moves](https://leetcode.cn/problems/maximize-the-topmost-element-after-k-moves/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[5,2,2,4,0,6]\\n4', '[2]\\n1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return (nums[0], -1)[k & 1]\n",
    "        if k < 2:\n",
    "            return nums[k]\n",
    "        if k > n:\n",
    "            return max(nums)\n",
    "        result = max(nums[:k-1])\n",
    "        return max(nums[k], result) if k < n else result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return -1 if k % 2 == 1 else nums[0]\n",
    "        \n",
    "        if k <= 1:\n",
    "            return nums[k]\n",
    "        \n",
    "        if k > n:\n",
    "            return max(nums)\n",
    "        \n",
    "        if k == n:\n",
    "            return max(nums[0:n-1])\n",
    "        \n",
    "        return max(max(nums[0:k-1]), nums[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return -1 if k%2==1 else nums[0]\n",
    "        if k <= 1:\n",
    "            return nums[k]\n",
    "        if k > n:\n",
    "            return max(nums)\n",
    "        if k == n:\n",
    "            return max(nums[0:n-1])\n",
    "        # 1 < k < n\n",
    "        return max(max(nums[0:k-1]),nums[k])\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        # n = 1且k为奇数时，最后一步刚好是pop操作，此时栈为空\n",
    "        if n == 1:\n",
    "            return -1 if k % 2 else nums[0]\n",
    "        \n",
    "        # 1. 若n > k，此时可以pop k次，最右可露出nums[k]\n",
    "        ans1 = nums[k] if n > k else 0\n",
    "        \n",
    "        # 2. 前面操作k - 1次(k > 1)，最后一步放回一个最大的数\n",
    "        ans2 = max(nums[:k - 1]) if k > 1 else 0\n",
    "        \n",
    "        return max(ans1, ans2)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        if k == 0:return nums[0]\n",
    "        n = len(nums)\n",
    "        if n == 1:return nums[0] if not k % 2 else -1\n",
    "        if k == 1:return nums[1]\n",
    "        if k > n:\n",
    "            return max(nums)\n",
    "        if k == n:return max(nums[:-1])\n",
    "        else:\n",
    "            mx = -1\n",
    "            for i in range(k-1):\n",
    "                mx = max(mx,nums[i])\n",
    "            mx = max(mx,nums[k])\n",
    "            return mx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        return max(num for i, num in enumerate(nums) if i < k - 1 or i == k) if len(nums) > 1 or k&1 == 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        if k==0:\n",
    "            return nums[0]\n",
    "        if len(nums)==1:\n",
    "            if k&1:\n",
    "                return -1\n",
    "        if k==1:\n",
    "            if len(nums)>1:\n",
    "                return nums[1]\n",
    "            else:\n",
    "                return -1\n",
    "        if k>len(nums):\n",
    "            return max(nums)\n",
    "        elif k==len(nums):\n",
    "            return max(nums[:-1])\n",
    "        return max(nums[:k-1]+[nums[k]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        if k == 0:\n",
    "            return nums[0]\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            if k % 2:\n",
    "                return -1\n",
    "            return nums[0]\n",
    "        if k == 1:\n",
    "            return nums[1]\n",
    "        if k > n:\n",
    "            return max(nums)\n",
    "        return max(nums[:k-1]+nums[k:k+1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return -1 if k % 2 else nums[0]\n",
    "\n",
    "        if k <= 1:\n",
    "            return nums[k]\n",
    "\n",
    "        ans = max(nums[:k - 1])\n",
    "        if k < n:\n",
    "            ans = max(ans, nums[k])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return nums[0] if k%2 == 0 else -1\n",
    "        if k <= 1:\n",
    "            return nums[k]\n",
    "        if k > n:\n",
    "            return max(nums)\n",
    "        if k == n:\n",
    "            return max(nums[:k-1])\n",
    "        # 1 < k < n\n",
    "        # nums = [5,2,2,4,0,6], k = 6\n",
    "        return max(max(nums[:k-1]),nums[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        if k == 0:\n",
    "            return nums[0]\n",
    "        if len(nums) == 1:\n",
    "            return nums[0] if k%2==0 else -1\n",
    "        if k == 1:\n",
    "            return nums[1]\n",
    "        if k < len(nums):\n",
    "            return max(nums[k], max(nums[:k-1]))\n",
    "        if k == len(nums):\n",
    "            return max(nums[:k-1])\n",
    "        return max(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "\n",
    "\n",
    "\n",
    "        n = len(nums)\n",
    "        res = -1\n",
    "        if n == 1:\n",
    "\n",
    "            if k % 2 == 0: return nums[0]\n",
    "            \n",
    "        \n",
    "        else:\n",
    "            for i in range(min(n, k + 1)):\n",
    "                if i != k - 1:\n",
    "                    res = max(res, nums[i])\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        if len(nums) == 1: \n",
    "            if k % 2==0: return nums[0]\n",
    "            else: return -1\n",
    "        del_nums = nums[:k-1] if k > 0 else []\n",
    "        cur_nums = nums[k:]\n",
    "        if len(cur_nums) == 0:\n",
    "            if len(del_nums) == 0:\n",
    "                res = -1\n",
    "            else: res = max(del_nums)\n",
    "        else:\n",
    "            if len(del_nums) == 0:\n",
    "                res = cur_nums[0]\n",
    "            else:\n",
    "                res = max(cur_nums[0], max(del_nums))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        #贪心思想\n",
    "        if len(nums)==1 and k%2==1: return -1\n",
    "        if k<=1: return nums[k]\n",
    "        a=max(nums[:k-1])\n",
    "        if k<len(nums):\n",
    "            a=max(a,nums[k])\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            if k % 2 == 1:\n",
    "                return -1\n",
    "            else:\n",
    "                return nums[0]\n",
    "        if k < 0:\n",
    "            return -1\n",
    "\n",
    "        if k == 0:\n",
    "            return nums[0]\n",
    "        \n",
    "        if k == 1:\n",
    "            if n <= 1:\n",
    "                return -1\n",
    "            return nums[1]\n",
    "        \n",
    "        if n >= k+1:\n",
    "            return max(max(nums[:k-1]), nums[k])\n",
    "        else:\n",
    "            return max(nums[:min(k-1, n)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        return max(num for i, num in enumerate(nums) if i < k - 1 or i == k) if len(nums) > 1 or k % 2 == 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        # n = 1且k为奇数时，最后一步刚好是pop操作，此时栈为空\n",
    "        if n == 1:\n",
    "            return -1 if k % 2 else nums[0]\n",
    "        \n",
    "        # 1. 若n > k，此时可以pop k次，最右可露出nums[k]\n",
    "        ans1 = nums[k] if n > k else 0\n",
    "        # 2. 前面操作k - 1次(k > 1)，最后一步放回一个最大的数\n",
    "        ans2 = max(nums[:min(n, k - 1)]) if k > 1 else 0\n",
    "        return max(ans1, ans2)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "\n",
    "\n",
    "        n = len(nums)\n",
    "        res = -1\n",
    "        if n == 1:\n",
    "\n",
    "            if k % 2 == 0: return nums[0]\n",
    "            \n",
    "        \n",
    "        else:\n",
    "            for i in range(min(n, k + 1)):\n",
    "                if i != k - 1:\n",
    "                    res = max(res, nums[i])\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        res = -1   # 最大的栈顶元素\n",
    "        if n == 1:\n",
    "            # n = 1 时若 k 为奇数则栈最终为空，此时返回 -1\n",
    "            if k % 2 == 0:\n",
    "                return nums[0]\n",
    "        else:\n",
    "            for i in range(min(n, k + 1)):\n",
    "                if i != k - 1:\n",
    "                    res = max(res, nums[i])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        if k == 0:\n",
    "            return nums[0]\n",
    "        if len(nums) == 1:\n",
    "            if k % 2 != 0:\n",
    "                return -1 \n",
    "            else:\n",
    "                return nums[0]\n",
    "        if k <= len(nums):\n",
    "            # 先找前k-1个数的最大值\n",
    "            if nums[:k-1]:\n",
    "                if k < len(nums):\n",
    "                    return max(max(nums[:k-1]),nums[k])\n",
    "                else:\n",
    "                    return max(nums[:k-1])\n",
    "            else:\n",
    "                if len(nums) <= 1:\n",
    "                    return -1\n",
    "                else:\n",
    "                    return nums[1]\n",
    "        if k > len(nums):\n",
    "            return max(nums)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1 and k & 1:\n",
    "            return -1\n",
    "        j = (0, k)[n > k]\n",
    "        for i in range(min(n, k)):\n",
    "            if nums[i] >= nums[j] and (k == i or k >= i + 2):\n",
    "                j = i\n",
    "        return nums[j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return nums[0] if k % 2 == 0 else -1\n",
    "        if k < n:\n",
    "            return max(max(nums[:k-1] if k > 1 else [0]), nums[k])\n",
    "        elif k == n:\n",
    "            return max(nums[:k-1])\n",
    "        return max(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        #贪心思想\n",
    "        if len(nums)==1 and k%2==1: return -1\n",
    "        if k<=1: return nums[k]\n",
    "        a=max(nums[:k-1])\n",
    "        if k<len(nums):\n",
    "            a=max(a,nums[k])\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "\n",
    "\n",
    "\n",
    "        n = len(nums)\n",
    "        res = -1\n",
    "        if n == 1:\n",
    "\n",
    "            if k % 2 == 0: return nums[0]\n",
    "            \n",
    "        \n",
    "        else:\n",
    "            for i in range(min(n, k + 1)):\n",
    "                if i != k - 1:\n",
    "                    res = max(res, nums[i])\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            if k % 2 == 0:\n",
    "                return nums[0]\n",
    "            else:\n",
    "                return -1\n",
    "\n",
    "        if k == 0:\n",
    "            return nums[0]\n",
    "        elif k == 1:\n",
    "            return nums[1]\n",
    "        elif k < n:\n",
    "            return max(nums[k], max(nums[:k - 1]))\n",
    "        elif k == n:\n",
    "            return max(nums[:n - 1])\n",
    "        else:\n",
    "            return max(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        # pop k - 1次，放回一个最大的数\n",
    "        # pop k次，剩余nums[k]\n",
    "        n = len(nums)\n",
    "        if n == 1 and k % 2 == 1:   return -1\n",
    "        ans1 = nums[k] if n > k else 0\n",
    "        ma = max(nums[:k - 1]) if k > 1 else 0\n",
    "        return max(ans1, ma)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            if k == 0:\n",
    "                return nums[0]\n",
    "            else:\n",
    "                if k%2==1:\n",
    "                    return -1\n",
    "                else:\n",
    "                    return nums[0]\n",
    "        else:\n",
    "            max_ = 0\n",
    "            for i in range(min(n,k-1)):\n",
    "                max_ = max(max_,nums[i])\n",
    "            if k >= n:\n",
    "                return max_\n",
    "            else:\n",
    "                return max(max_,nums[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        if len(nums) == 1 and k & 1:\n",
    "            return -1\n",
    "        if k == 0:\n",
    "            return nums[0]\n",
    "        \n",
    "        res = []\n",
    "        if k < len(nums):\n",
    "            res.append(nums[k])\n",
    "        res.extend(nums[:k-1])\n",
    "\n",
    "        return max(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        res = -1   # 最大的栈顶元素\n",
    "        if n == 1:\n",
    "            # n = 1 时若 k 为奇数则栈最终为空，此时返回 -1\n",
    "            if k % 2 == 0:\n",
    "                return nums[0]\n",
    "        else:\n",
    "            for i in range(min(n, k + 1)):\n",
    "                if i != k - 1:\n",
    "                    res = max(res, nums[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return -1 if k % 2 else nums[0]\n",
    "        # pop k次，剩余nums[k]\n",
    "        ans1 = nums[k] if n > k else 0\n",
    "        # pop k - 1次，放回一个最大的数\n",
    "        ma = max(nums[:k - 1]) if k > 1 else 0\n",
    "        return max(ans1, ma)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return -1 if k % 2 else nums[0]\n",
    "        \n",
    "        # 1. pop k次，剩余nums[k]\n",
    "        ans1 = nums[k] if n > k else 0\n",
    "        # 2. pop k - 1次，放回一个最大的数\n",
    "        ans2 = max(nums[:k - 1]) if k > 1 else 0\n",
    "        return max(ans1, ans2)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        res = -1   # 最大的栈顶元素\n",
    "        if n == 1:\n",
    "            # n = 1 时若 k 为奇数则栈最终为空，此时返回 -1\n",
    "            if k % 2 == 0:\n",
    "                return nums[0]\n",
    "            else:\n",
    "                return res\n",
    "        else:\n",
    "            if k > 1:\n",
    "                res = max(nums[:min(n, k-1)])\n",
    "            res = max(res, nums[k]) if k < n else res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        \n",
    "        # 如果k为0，返回第一个元素\n",
    "        if k == 0:\n",
    "            return nums[0]\n",
    "\n",
    "        # 如果k为1且数组长度大于1，返回第二个元素\n",
    "        if k == 1:\n",
    "            return nums[1] if n > 1 else -1\n",
    "\n",
    "        # 如果数组长度为1且k是奇数，无法在k次操作后保持数组非空\n",
    "        if n == 1:\n",
    "            return -1 if k % 2 == 1 else nums[0]\n",
    "\n",
    "        # 如果k大于数组长度，直接返回数组中的最大值\n",
    "        if k > n:\n",
    "            return max(nums)\n",
    "\n",
    "        # 如果k等于数组长度，返回数组中除最后一个元素外的最大值\n",
    "        if k == n:\n",
    "            return max(nums[:-1])\n",
    "\n",
    "        # 其他情况，返回前k个元素中的最大值和第k个元素中的较大者\n",
    "        return max(max(nums[:k-1]), nums[k] if k < n else float('-inf'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        res = -1   # 最大的栈顶元素\n",
    "        if n == 1:\n",
    "            # n = 1 时若 k 为奇数则栈最终为空，此时返回 -1\n",
    "            if k % 2 == 0:\n",
    "                return nums[0]\n",
    "        else:\n",
    "            for i in range(min(n, k + 1)):\n",
    "                if i != k - 1:\n",
    "                    res = max(res, nums[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        if k == 0:\n",
    "            return nums[0]\n",
    "        if len(nums) == 1:\n",
    "            return -1 if k % 2 else nums[0]\n",
    "        if k == 1:\n",
    "            return nums[1]\n",
    "        res = max(nums[:k-1])\n",
    "        if k < len(nums):\n",
    "            res = max(res, nums[k])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        if len(nums) == 1:\n",
    "            return nums[0] if k & 1 == 0 else -1\n",
    "        \n",
    "        if k > len(nums):\n",
    "            return max(nums)\n",
    "        \n",
    "        if k == 1:\n",
    "            return nums[1]\n",
    "        \n",
    "        res = nums[0]\n",
    "        for i in range(1, k - 1):\n",
    "            res = max(nums[i], res)\n",
    "        \n",
    "        if k == len(nums):\n",
    "            return res\n",
    "        res = max(res, nums[k])\n",
    "        \n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        if k == 0:\n",
    "            return nums[0]\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            if k % 2:\n",
    "                return -1\n",
    "            return nums[0]\n",
    "        ans = max(nums[: k - 1], default=-1)\n",
    "        if k < n:\n",
    "            ans = max(ans, nums[k])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        \n",
    "        n = len(nums)\n",
    "        \n",
    "        if n == 1:\n",
    "            return -1 if k % 2 == 1 else nums[0]\n",
    "\n",
    "        if k <= 1:\n",
    "            return nums[k]\n",
    "\n",
    "        if k > n:\n",
    "            return max(nums)\n",
    "\n",
    "        if k == n:\n",
    "            return max(nums[0: n - 1])\n",
    "\n",
    "        # 1 < k < n\n",
    "        return max(max(nums[0: k - 1]), nums[k])\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        max_deleted = float('-inf')\n",
    "        if k > len(nums):\n",
    "            if len(nums) == 1:\n",
    "                if k % 2 == 0:\n",
    "                    return nums[0]\n",
    "                else:\n",
    "                    return -1\n",
    "            else:\n",
    "                return max(nums)\n",
    "        elif k == len(nums):\n",
    "            if k == 0 or k == 1:\n",
    "                return -1\n",
    "            else:\n",
    "                return max(nums[:-1])\n",
    "        else:\n",
    "            if k == 0:\n",
    "                return nums[0]\n",
    "            while k > 1:\n",
    "                num = nums.pop(0)\n",
    "                if num > max_deleted:\n",
    "                    max_deleted = num\n",
    "                k -= 1\n",
    "            if max_deleted > nums[1]:\n",
    "                return max_deleted\n",
    "            else:\n",
    "                return nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if k == 0:\n",
    "            return nums[0]\n",
    "        if n == 1:\n",
    "            if k % 2:\n",
    "                return -1\n",
    "            return nums[0]\n",
    "\n",
    "        ans = max(nums[:k - 1], default=-1)\n",
    "        if k < n:\n",
    "            ans = max(ans, nums[k])\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "\n",
    "        n = len(nums)\n",
    "        # print(n)\n",
    "        if n == 1:\n",
    "            return -1 if k%2 else nums[0]\n",
    "        elif n == 0:\n",
    "            return -1\n",
    "        \n",
    "        if k == 1:\n",
    "            return nums[1] if n >= 2 else -1\n",
    "        elif k == 0:\n",
    "            return nums[0]\n",
    "\n",
    "\n",
    "        if n > k:\n",
    "            return max(max(nums[:k-1]),nums[k])\n",
    "        elif n == k:\n",
    "            return max(nums[:k-1])\n",
    "        else:\n",
    "            return max(nums)\n",
    "        \n",
    "\n",
    "        # q = deque(nums)\n",
    "        # print(q.popleft())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        res = -1\n",
    "        if n == 1:\n",
    "            if k % 2 == 0:\n",
    "                return nums[0]\n",
    "        else:\n",
    "            for i in range(min(n, k + 1)):\n",
    "                if i != k - 1:\n",
    "                    res = max(res, nums[i])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumTop(self, nums: List[int], k: int) -> int:\n",
    "        l=len(nums)\n",
    "        if l==1 and k%2==1:\n",
    "            return -1\n",
    "        if k==1:\n",
    "            return nums[1]    \n",
    "        nums[::]=nums[:k+1]\n",
    "        index=nums.index(max(nums))\n",
    "        if k!=index+1:\n",
    "            return max(nums)\n",
    "        if index+1<len(nums):\n",
    "            return max(max(nums[:index]),nums[index+1])\n",
    "        return max(nums[:index])"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
